package com.labofjet.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.labofjet.bean.impl.EState;
import com.labofjet.dto.ADTO;
import com.labofjet.entity.A;
import com.labofjet.entity.Student;
import com.labofjet.repository.module1.ARepository;
import com.labofjet.repository.module2.PhoneRepository;
import com.labofjet.service.TestService;
import com.labofjet.service.TestTransactionService1;
import com.labofjet.util.ConverterUtils;
import com.labofjet.util.MessageUtils;
import com.labofjet.web.bean.impl.JsonContextDTO;

@Controller
// @SessionAttributes(value = { "modelArrt" })
public class TestController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;
    @Autowired
    private ARepository arepo;
    @Autowired
    private PhoneRepository phoneRepository;

    @Autowired
    TestTransactionService1 testTransactionService1;

    @RequestMapping("/test")
    @ResponseBody
    public String index(long id) {
        LOGGER.debug("hello {}", "jet");
        return testService.findById(id).toString();
    }

    @RequestMapping("/test2")
    @ResponseBody
    public String index2(String name) {
        return testService.findByName(name).toString();
    }

    @RequestMapping("/test3")
    @ResponseBody
    public Object index3(String name) {
        return testService.findStudentByPhonesPhoneName(name).toString();
    }

    @RequestMapping("/test4")
    @ResponseBody
    public Object index4(long id) {
        return testService.deleteStudentById(id).toString();
    }

    @RequestMapping("/test5")
    @ResponseBody
    public Object index5(long num) {
        Page<Student> page = testService.findAll(new PageRequest(1, (int) num));
        return page.getContent().toString();
    }

    @RequestMapping("/test6")
    @ResponseBody
    public Object index6() {
        List<Student> page = testService.findByIds(Arrays.asList(1l, 2l));
        return page.toString();
    }

    @RequestMapping("/test7")
    @ResponseBody
    public Object index7() {
        List<Student> page = testService.findTop2(new PageRequest(1, 1));
        return page.toString();
    }

    @RequestMapping("/test8")
    @ResponseBody
    public Object index8() {
        List<Student> page = testService.findTop3ByOrderByName(new PageRequest(1, 1));
        return page.toString();
    }

    @RequestMapping("/test9")
    @ResponseBody
    public Object index9() {
        Student s = testService.saveOne();
        return s.toString();
    }

    @RequestMapping("/test10")
    @ResponseBody
    public Object index10() {
        // 删除孤子
        Student s = testService.saveOne();
        Student newStu = new Student();
        newStu.setId(s.getId());
        newStu.setName("test10");
        newStu.setPhones(new ArrayList());
        testService.updateOne(newStu);
        return null;
    }

    @RequestMapping("/test11")
    @ResponseBody
    public Object index11() {
        // 测试更新
        Student s = testService.findById(2);
        s.setName(s.getName() + "_" + s.getId());
        s.setId(1);
        testService.saveOne(s);
        return null;
    }

    @RequestMapping("/test12")
    @ResponseBody
    public void index12() {
        // 测试主键
        // 结论:判断2个对象是不是同一个不是通过equals或者hashcode判断的,可能是通过SQL来判断的,有记录就是需要update,没有就是insert
        // A a = arepo.findOne(new APK("1","name1"));
        // "1".equals("2");
        // A a2 = new A();
        // a2.setId(new APK("3", "name3"));
        // a2.setAge("23");
        // arepo.save(a2);
    }

    @RequestMapping("/test13")
    @ResponseBody
    public Object index13() {
        // 测试存储过程
        // Object o = arepo.alias3(123);
        // return null;
        Object[] o = arepo.mytest();
        return null;
    }

    @RequestMapping("/test14")
    @ResponseBody
    public Object index14(@RequestBody JsonContextDTO a, HttpSession s) {
        JsonContextDTO dto = new JsonContextDTO();
        return dto;
    }

    /**
     * 测试前后台数据交互格式统一
     * 
     * @param context
     * @return
     * @author jiangyz
     */
    @RequestMapping("/test15")
    @ResponseBody
    public Object index15(@RequestBody JsonContextDTO context) {
        A a = context.getDto("aName", A.class);
        JsonContextDTO dto = new JsonContextDTO();
        dto.putData("aDataName", a);
        dto.setStateCode(EState.SUCCESS.getCode());
        throw new ArrayIndexOutOfBoundsException();
        // throw new BusinessException("999", "缴费人数为负数");
        // return dto;
    }

    /**
     * 测试原生SQL
     * 
     * @param context
     * @return
     * @author jiangyz
     */
    @RequestMapping("/test16")
    @ResponseBody
    public Object index16(JsonContextDTO context, HttpSession s) {
        // HashMap map = new HashMap<String, String>();
        // map.put("age", "23");
        // Object o = arepo.querySingle("select * from a where age = :age",
        // map);
        // context.putData("obj", o);
    	s.setAttribute("test", "test!哈哈");
        return context;
    }

    @RequestMapping("/test17")
    @ResponseBody
    public Object index17(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getHeaderNames();
        response.getWriter();
        return null;
    }

    @RequestMapping("/around")
    @ResponseBody
    public Object around(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "e";
    }

    @RequestMapping("/around2")
    @ResponseBody
    public Object around2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return around(null, null);
    }

    @RequestMapping("/test18")
    public Object index18(JsonContextDTO context, ADTO a) throws IOException {
        System.out.println(context);
        System.out.println(a);
        return null;
    }

    @RequestMapping("/test19")
    public Object index19() throws IOException {
        return "index2";
    }

    /**
     * Spring 声明式事务学习
     * 
     * @return
     * @throws IOException
     * @author jiangyz
     */
    @RequestMapping("/test20")
    public Object index20() throws IOException {
        testTransactionService1.operate1();
        return null;
    }

    /**
     * Message学习
     * 
     * @return
     * @throws IOException
     * @author jiangyz
     */
    @RequestMapping("/test21")
    public Object index21() {
        System.out.println(MessageUtils.getMessage("m1"));
        System.out.println(MessageUtils.getMessage("m2", "defaultMessage"));
        System.out.println(MessageUtils.getMessage("m1", new Object[] { "jet", "bony" }));
        return null;
    }

    @RequestMapping("/test22")
    public Object index22() {
        testTransactionService1.operate3();
        return null;
    }

    @RequestMapping("/test23")
    public Object index23(@RequestBody ADTO a) {
        System.out.println(a);
        return null;
    }

    @RequestMapping("/test24")
    @ResponseBody
    public Object test24(HttpServletResponse res) throws IOException {
        return "{\"a\":\"哈哈\"}";
    }

    @ModelAttribute
    public void before(Model m) {
        m.addAttribute("modelArrt", "666");
    }

    @RequestMapping("/test25")
    public Object test25(Model m, String modelArrt, HttpSession session) throws IOException {
        System.out.println(m);
        System.out.println(modelArrt);
        System.out.println(session.getAttribute("modelArrt"));
        // m.addAttribute("modelArrt", new Random().nextInt());
        return "{\"a\":\"哈哈\"}";
    }

    @RequestMapping("/*")
    public Object star(Model m, HttpSession session) throws IOException {
        return "{\"a\":\"哈哈\"}";
    }

    @RequestMapping("/test27")
    @ResponseBody
    public Object test27() {
        JsonContextDTO context = new JsonContextDTO();
        Student s = testService.findById(15);
        Student copy = ConverterUtils.convertOne(s, new Student());
        //System.out.println(s.getPhones());
        //context.putData("student", s);
        return copy;
    }
    
    @RequestMapping("/test28")
    @ResponseBody
    public Object test28() {
        testService.testTransaction();
        return null;
    }
}
